import { Request, Response, NextFunction } from 'express'
import { ZodError } from 'zod'
import { NotFoundError, ParameterError, RejectError, UnauthorizedError } from './errors'

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function errorHandler(err: unknown, req: Request, res: Response, next: NextFunction) {
  if (err instanceof ZodError) {
    return res.status(400).json({ error: err.issues })
  }
  if (err instanceof ParameterError) {
    return res.status(400).json({ error: err.message })
  }
  if (err instanceof RejectError) {
    return res.status(403).json({ error: err.message })
  }
  if (err instanceof UnauthorizedError) {
    return res.status(401).json({ error: err.message })
  }
  if (err instanceof NotFoundError) {
    return res.status(404).json({ error: err.message })
  }
  if (err instanceof Error) {
    return res.status(500).json({ error: err.message })
  }
  return res.status(500).send('Something broke!')
}
